package com.faithbj.webservice;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;

import com.faithbj.common.model.compare.AFMOfficalData;
import com.faithbj.common.model.compare.CivilOfficalData;
import com.faithbj.common.model.compare.HPOfficalData;
import com.faithbj.common.model.compare.IBAOfficalData;
import com.faithbj.common.model.compare.PopulationOfficalData;
import com.faithbj.common.model.compare.SIOfficalData;
import com.faithbj.common.model.compare.TaxOfficalData;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.common.utils.SpringUtil;
import com.faithbj.constant.Constant;
import com.faithbj.service.CompareService;
import com.faithbj.support.vo.AFMCompareResponse;
import com.faithbj.support.vo.AFMCompareResponse.AFMData;
import com.faithbj.support.vo.CivilCompareResponse;
import com.faithbj.support.vo.CivilCompareResponse.CivilData;
import com.faithbj.support.vo.CompareRequest;
import com.faithbj.support.vo.CompareRequest.Applicant;
import com.faithbj.support.vo.HPCompareResponse;
import com.faithbj.support.vo.HPCompareResponse.HPData;
import com.faithbj.support.vo.IBACompareResponse;
import com.faithbj.support.vo.IBACompareResponse.IBAData;
import com.faithbj.support.vo.PSCompareResponse;
import com.faithbj.support.vo.PSCompareResponse.PSData;
import com.faithbj.support.vo.SICompareResponse;
import com.faithbj.support.vo.SICompareResponse.SIData;
import com.faithbj.support.vo.TaxCompareResponse;
import com.faithbj.support.vo.TaxCompareResponse.TaxData;

/**
 * Webservice 客户端
 * 
 * @author chen liu
 */
public class WebServiceClient {
    private static ApplicationContext context;
    private static WebServiceServer client;
    private static CompareService compareService;
    static {
        context = SpringUtil.getApplicationContext();
        client = context.getBean("webServiceClient", WebServiceServer.class);
        compareService = SpringUtil.getApplicationContext().getBean(CompareService.class);
    }

    // 发送委办请求
    public static void sendRequest(Map<String, List<CompareRequest>> requestMap) {
        for (String depNum : requestMap.keySet()) {
            Date queryStartDt = DateUtils.getCurrentDate();
            boolean result = client.sendRequest(requestMap.get(depNum));
            if (result) {
                System.out.println("查询请求发送成功!");
                // 保存委办请求对象
                saveRequest(depNum, requestMap.get(depNum), queryStartDt);
            } else {
                System.out.println("查询请求发送失败!");
            }
        }

    }

    // 委办返回
    public static void returnOfficalData(String depNum, List<CompareRequest> requestList) {
        if (depNum.equals(Constant.DEP_NUM_AFM)) {
            sendAFMOfficalResponse(requestList);
        } else if (depNum.equals(Constant.DEP_NUM_CIVIAL)) {
            sendCivilOfficalResponse(requestList);
        } else if (depNum.equals(Constant.DEP_NUM_HP)) {
            sendHPOfficalResponse(requestList);
        } else if (depNum.equals(Constant.DEP_NUM_IBA)) {
            sendIBAOfficalResponse(requestList);
        } else if (depNum.equals(Constant.DEP_NUM_PS)) {
            sendPSOfficalResponse(requestList);
        } else if (depNum.equals(Constant.DEP_NUM_SIO)) {
            sendSIOfficalResponse(requestList);
        } else if (depNum.equals(Constant.DEP_NUM_TAX)) {
            sendTaxOfficalResponse(requestList);
        }
    }

    private static void sendAFMOfficalResponse(List<CompareRequest> requestList) {
        List<AFMCompareResponse> responseList = new ArrayList<AFMCompareResponse>();
        // 对每一个请求(以家庭为单位)构造委办数据返回
        for (CompareRequest req : requestList) {
            AFMCompareResponse afmResponse = new AFMCompareResponse(req.getBatchNum(), req.getDepartmentNum(),
                    req.getMainApplicantName(), req.getMainApplicantCardId());
            List<AFMData> list = new ArrayList<AFMData>();
            for (Applicant applicant : req.getApplicants()) {
                AFMData data = getAFMDataSample(applicant);
                list.add(data);
            }
            afmResponse.setMemberDatas(list);
            responseList.add(afmResponse);
        }
        boolean result = client.sendAFMResponse(responseList);
        if (result) {
            System.out.println("公积金委办数据发送成功!");
        }
    }
    
    private static void sendCivilOfficalResponse(List<CompareRequest> requestList) {
        List<CivilCompareResponse> responseList = new ArrayList<CivilCompareResponse>();
        // 对每一个请求(以家庭为单位)构造委办数据返回
        for (CompareRequest req : requestList) {
            CivilCompareResponse civilResponse = new CivilCompareResponse(req.getBatchNum(), req.getDepartmentNum(),
                    req.getMainApplicantName(), req.getMainApplicantCardId());
            List<CivilData> list = new ArrayList<CivilData>();
            for (Applicant applicant : req.getApplicants()) {
                CivilData data = getCivilDataSample(applicant);
                list.add(data);
            }
            civilResponse.setMemberDatas(list);
            responseList.add(civilResponse);
        }
        boolean result = client.sendCivilResponse(responseList);
        if (result) {
            System.out.println("民政局委办数据发送成功!");
        }
    }
    
    private static void sendHPOfficalResponse(List<CompareRequest> requestList) {
        List<HPCompareResponse> responseList = new ArrayList<HPCompareResponse>();
        // 对每一个请求(以家庭为单位)构造委办数据返回
        for (CompareRequest req : requestList) {
            HPCompareResponse hpResponse = new HPCompareResponse(req.getBatchNum(), req.getDepartmentNum(),
                    req.getMainApplicantName(), req.getMainApplicantCardId());
            List<HPData> list = new ArrayList<HPData>();
            for (Applicant applicant : req.getApplicants()) {
                HPData data = getHPDataSample(applicant);
                list.add(data);
            }
            hpResponse.setMemberDatas(list);
            responseList.add(hpResponse);
        }
        boolean result = client.sendHPResponse(responseList);
        if (result) {
            System.out.println("房产局委办数据发送成功!");
        }
    }
    
    private static void sendIBAOfficalResponse(List<CompareRequest> requestList) {
        List<IBACompareResponse> responseList = new ArrayList<IBACompareResponse>();
        // 对每一个请求(以家庭为单位)构造委办数据返回
        for (CompareRequest req : requestList) {
            IBACompareResponse ibaResponse = new IBACompareResponse(req.getBatchNum(), req.getDepartmentNum(),
                    req.getMainApplicantName(), req.getMainApplicantCardId());
            List<IBAData> list = new ArrayList<IBAData>();
            for (Applicant applicant : req.getApplicants()) {
                IBAData data = getIBADataSample(applicant);
                list.add(data);
            }
            ibaResponse.setMemberDatas(list);
            responseList.add(ibaResponse);
        }
        boolean result = client.sendIBAResponse(responseList);
        if (result) {
            System.out.println("工商行政管理委办数据发送成功!");
        }
    }
    
    private static void sendSIOfficalResponse(List<CompareRequest> requestList) {
        List<SICompareResponse> responseList = new ArrayList<SICompareResponse>();
        // 对每一个请求(以家庭为单位)构造委办数据返回
        for (CompareRequest req : requestList) {
            SICompareResponse siResponse = new SICompareResponse(req.getBatchNum(), req.getDepartmentNum(),
                    req.getMainApplicantName(), req.getMainApplicantCardId());
            List<SIData> list = new ArrayList<SIData>();
            for (Applicant applicant : req.getApplicants()) {
                SIData data = getSIDataSample(applicant);
                list.add(data);
            }
            siResponse.setMemberDatas(list);
            responseList.add(siResponse);
        }
        boolean result = client.sendSIResponse(responseList);
        if (result) {
            System.out.println("社会保险委办数据发送成功!");
        }
    }
    
    private static void sendTaxOfficalResponse(List<CompareRequest> requestList) {
        List<TaxCompareResponse> responseList = new ArrayList<TaxCompareResponse>();
        // 对每一个请求(以家庭为单位)构造委办数据返回
        for (CompareRequest req : requestList) {
            TaxCompareResponse taxResponse = new TaxCompareResponse(req.getBatchNum(), req.getDepartmentNum(),
                    req.getMainApplicantName(), req.getMainApplicantCardId());
            List<TaxData> list = new ArrayList<TaxData>();
            for (Applicant applicant : req.getApplicants()) {
                TaxData data = getTaxDataSample(applicant);
                list.add(data);
            }
            taxResponse.setMemberDatas(list);
            responseList.add(taxResponse);
        }
        boolean result = client.sendTaxResponse(responseList);
        if (result) {
            System.out.println("税务委办数据发送成功!");
        }
    }
    
    private static void sendPSOfficalResponse(List<CompareRequest> requestList) {
        List<PSCompareResponse> responseList = new ArrayList<PSCompareResponse>();
        // 对每一个请求(以家庭为单位)构造委办数据返回
        for (CompareRequest req : requestList) {
            PSCompareResponse psResponse = new PSCompareResponse(req.getBatchNum(), req.getDepartmentNum(),
                    req.getMainApplicantName(), req.getMainApplicantCardId());
            List<PSData> list = new ArrayList<PSData>();
            for (Applicant applicant : req.getApplicants()) {
                PSData data = getPSDataSample(applicant);
                list.add(data);
            }
            psResponse.setMemberDatas(list);
            responseList.add(psResponse);
        }
        boolean result = client.sendPSResponse(responseList);
        if (result) {
            System.out.println("公安局委办数据发送成功!");
        }
    }

    private static void saveRequest(String depNum, List<CompareRequest> requestList, Date queryStartDt) {
        if (depNum.equals(Constant.DEP_NUM_AFM)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    AFMOfficalData data = new AFMOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(),
                            request.getMainApplicantCardType(), request.getMainApplicantCardId(),
                            applicant.getApplicantName(), applicant.getApplicantCardType(),
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, queryStartDt,
                            DateUtils.getCurrentDate(), null);
                    System.out.println("保存:" + request.getApplyNum() + ":" + applicant.getApplicantCardId());
                    compareService.saveOrUpdateAFMOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_CIVIAL)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    CivilOfficalData data = new CivilOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(),
                            request.getMainApplicantCardType(), request.getMainApplicantCardId(),
                            applicant.getApplicantName(), applicant.getApplicantCardType(),
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, queryStartDt,
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateCivilOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_HP)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    HPOfficalData data = new HPOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(),
                            request.getMainApplicantCardType(), request.getMainApplicantCardId(),
                            applicant.getApplicantName(), applicant.getApplicantCardType(),
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, queryStartDt,
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateHPOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_IBA)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    IBAOfficalData data = new IBAOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(),
                            request.getMainApplicantCardType(), request.getMainApplicantCardId(),
                            applicant.getApplicantName(), applicant.getApplicantCardType(),
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, queryStartDt,
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateIBAOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_PS)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    PopulationOfficalData data = new PopulationOfficalData(request.getApplyNum(),
                            request.getBatchNum(), request.getDepartmentNum(), request.getMainApplicantName(),
                            request.getMainApplicantCardType(), request.getMainApplicantCardId(),
                            applicant.getApplicantName(), applicant.getApplicantCardType(),
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, queryStartDt,
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdatePSOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_SIO)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    SIOfficalData data = new SIOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(),
                            request.getMainApplicantCardType(), request.getMainApplicantCardId(),
                            applicant.getApplicantName(), applicant.getApplicantCardType(),
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, queryStartDt,
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateSIOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_TAX)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    TaxOfficalData data = new TaxOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(),
                            request.getMainApplicantCardType(), request.getMainApplicantCardId(),
                            applicant.getApplicantName(), applicant.getApplicantCardType(),
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, queryStartDt,
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateTaxOfficalData(data);
                }
            }
        }
    }

    // 公积金样本数据
    private static AFMData getAFMDataSample(Applicant applicant) {
        AFMData afmData = new AFMData();
        afmData.setApplicantName(applicant.getApplicantName());
        afmData.setApplicantCardType(applicant.getApplicantCardType());
        afmData.setApplicantCardNum(applicant.getApplicantCardId());
        afmData.setAccumulationFundName(applicant.getApplicantName());

        // sample数据
        afmData.setPersonalAccountNum("123456789");
        afmData.setCompanyCode("01");
        afmData.setMonthlyDepositAmount(new BigDecimal(1000.10));
        afmData.setFixedBalanceAmount(new BigDecimal(10000.00));
        afmData.setCurrentBalanceAmount(new BigDecimal(2000.34));
        afmData.setLastDepositDt(DateUtils.getCurrentDate());
        afmData.setStatus(1);
        afmData.setHasSymbol(1);
        afmData.setWithdrawType("001");
        afmData.setWithdrawDt(DateUtils.getCurrentDate());
        afmData.setWithdrawAmount(new BigDecimal(500.12));
        afmData.setWithdrawInterests(new BigDecimal(24.24));
        afmData.setWithdrawReasonCode("01");
        afmData.setBankName("中国工商银行");
        return afmData;
    }

    // 民政局样本数据
    private static CivilData getCivilDataSample(Applicant applicant) {
        CivilData civilData = new CivilData();
        civilData.setApplicantName(applicant.getApplicantName());
        civilData.setApplicantCardType(applicant.getApplicantCardType());
        civilData.setApplicantCardNum(applicant.getApplicantCardId());

        // sample数据
        civilData.setName(applicant.getApplicantName());
        civilData.setGender("01");
        civilData.setAge(30);
        civilData.setNation("36");
        civilData.setDeathDt(null);
        civilData.setLowIncomeAidMoney(new BigDecimal(2000.00));
        civilData.setSpecialCareAidMoney(new BigDecimal(1500.90));
        civilData.setSpouseName("丁一");
        civilData.setSpouseCardNum("37028219569658");
        civilData.setSpouseCountry("01");
        civilData.setSpouseHouseholdAddr("01");
        civilData.setMaritalStatus("01");
        civilData.setMarriedDt(DateUtils.getCurrentDate());
        return civilData;
    }

    // 房产局样本数据
    private static HPData getHPDataSample(Applicant applicant) {
        HPData hpData = new HPData();
        hpData.setApplicantName(applicant.getApplicantName());
        hpData.setApplicantCardType(applicant.getApplicantCardType());
        hpData.setApplicantCardNum(applicant.getApplicantCardId());

        // sample数据
        hpData.setHouseCard("10001");
        hpData.setHouseLocation("北京市海淀区西土城路5号");
        hpData.setHouseBuildingArea(new BigDecimal(90.00));
        hpData.setHouseType("02");
        hpData.setHousePropertyType("01");
        hpData.setIsLivingHouse(1);
        hpData.setHouseSharePeople("丁四");
        return hpData;
    }
    
    // 工商行政管理样本数据
    private static IBAData getIBADataSample(Applicant applicant) {
        IBAData ibaData = new IBAData();
        ibaData.setApplicantName(applicant.getApplicantName());
        ibaData.setApplicantCardType(applicant.getApplicantCardType());
        ibaData.setApplicantCardNum(applicant.getApplicantCardId());
        
        ibaData.setCompanyName("沈阳丁浩科技有限公司");
        ibaData.setRegNum("10001");
        ibaData.setCompanyAddr("沈阳市市北区天山路5号");
        ibaData.setLegalRepresentative("王五");
        ibaData.setRegFunds(new BigDecimal(1000000.00));
        ibaData.setRealRecieveCapital(new BigDecimal(900000.00));
        ibaData.setEconomicNature("私营企业");
        ibaData.setBusinessPractice("咨询服务");
        ibaData.setBusinessScope("IT");
        ibaData.setLicenseIssuingOrgan("沈阳市国税局");
        ibaData.setLicenseIssuingDt(DateUtils.getCurrentDate());
        ibaData.setFoundDt(DateUtils.getCurrentDate());
        ibaData.setBusinessStartDt(DateUtils.getCurrentDate());
        ibaData.setBusinessEndDt(null);
        return ibaData;
    }
    
    // 公安局样本数据
    private static PSData getPSDataSample(Applicant applicant) {
        PSData psData = new PSData();
        psData.setApplicantName(applicant.getApplicantName());
        psData.setApplicantCardType(applicant.getApplicantCardType());
        psData.setApplicantCardNum(applicant.getApplicantCardId());
        psData.setName(applicant.getApplicantName());
        
        psData.setGender("01");
        psData.setBirthday(DateUtils.getDaysAgo(365 * 30));
        psData.setIdType(applicant.getApplicantCardType());
        psData.setIdNumber(applicant.getApplicantCardId());
        psData.setHouseholdType("01");
        psData.setRelWithHouseholder("02");
        psData.setNation("01");
        psData.setHomeAddr("北京市海淀区西土城路5号");
        psData.setBirthplace("沈阳市");
        psData.setNativePlace("沈阳市");
        psData.setIsNative(0);
        psData.setEducation("01");
        psData.setMarried(1);
        psData.setCareer("IT");
        psData.setHouseholdChangeReason(null);
        psData.setHouseholdDistrictCode("沈阳市");
        psData.setIsDead(2);
        psData.setDeathDt(null);
        psData.setCremationDep("沈阳市殡葬厂一分队");
        psData.setCremationDt(null);
        psData.setCarOwner("王五");
        psData.setCarPlateType("01");
        psData.setCarPlateNum("20001");
        psData.setCarBrand("K23");
        psData.setCarModel("01");
        psData.setCarType("01");
        psData.setCarUseProperty("私家车");
        psData.setCarStatus(1);
        psData.setCarRegDt(DateUtils.getCurrentDate());
        psData.setCarExamineDt(DateUtils.getCurrentDate());
        psData.setCarOwnerChanges(null);
        psData.setCarReturnDt(null);
        psData.setLivingAddr("北京市海淀区西土城路5号");
        return psData;
    }
    
    // 社会保险样本数据
    private static SIData getSIDataSample(Applicant applicant) {
        SIData siData = new SIData();
        siData.setApplicantName(applicant.getApplicantName());
        siData.setApplicantCardType(applicant.getApplicantCardType());
        siData.setApplicantCardNum(applicant.getApplicantCardId());
        
        siData.setSocialInsuranceName(applicant.getApplicantName());
        siData.setPersonalAttribute("01");
        siData.setPayCompany("沈阳丁浩科技有限公司");
        siData.setPayDt(DateUtils.getCurrentDate());
        siData.setMedicalPay(new BigDecimal(1000.34));
        siData.setUnemploymentPay(new BigDecimal(2000.67));
        siData.setRetirementPay(new BigDecimal(3000.87));
        siData.setAvgIncomeLastYear(new BigDecimal(1500.00));
        return siData;
    }

    // 税务样本数据
    private static TaxData getTaxDataSample(Applicant applicant) {
        TaxData taxData = new TaxData();
        taxData.setApplicantName(applicant.getApplicantName());
        taxData.setApplicantCardType(applicant.getApplicantCardType());
        taxData.setApplicantCardNum(applicant.getApplicantCardId());

        taxData.setTaxerName(applicant.getApplicantName());
        taxData.setCompanyName("沈阳丁浩科技有限公司");
        taxData.setCompanyRegAddr("沈阳市市北区天山路5号");
        taxData.setCompanyOperateAddr("沈阳市市北区天山路5号");
        taxData.setCompanyPhone("08989");
        taxData.setTaxAuthorityName("沈阳市国税局");
        taxData.setDeclareDate(DateUtils.getCurrentDate());
        taxData.setTaxType("01");
        taxData.setTaxItem("企业所得税");
        taxData.setIncome(new BigDecimal(100000.00));
        taxData.setTaxAmount(new BigDecimal(10000.00));
        return taxData;
    }
}
